clc; clear all; close all;

global switch_figure;

switch_figure = 0;  %1 or 0

mymoment_cell = {'deviation','freq_any_h1_mean','freq_any_h3_mean','freq_any_h12_mean','freq_any_h24_mean'};

NNbins = 101;

path_str = "";
Table_data = readtable((path_str + "CompRefwoHeterogeneity.csv"));
for jj=1:length(mymoment_cell)
        eval(['USCell.' mymoment_cell{jj} 'Vec= Table_data.' mymoment_cell{jj} '(2:' num2str(NNbins) '-1)./100;']);
end
eval(['USCell.densityVec= Table_data.density(2:' num2str(NNbins) '-1)./' ... 
          'sum(Table_data.density(2:' num2str(NNbins) '-1));']);
eval(['USCell.lengthA =' num2str(NNbins) ';']);

%Call the function that does the decomposition

for hh=[1 3 12 24]
    USCell=CaballeroEngelDecomposition_func(USCell,hh);
end

marginsCell = {'intensive','gross extensive','selection','sumAll','LambdaPrimebar','absLambdaPrimebarPlus','absLambdaPrimebarMinus'};
filename = 'CEDecompResults.xlsx';
%Create an xls table with the results
%First write the header
xlswrite(filename,marginsCell,1,'B1');

ii=1;
for hh=[1 3 12 24]
 eval(['xlsRow = {hh,USCell.intensive_h' num2str(hh) ',USCell.grossExtensive_h' num2str(hh) ',USCell.selection_h' num2str(hh) ...
     ',USCell.sumAll_h' num2str(hh) ',USCell.LambdaPrimebar_h' num2str(hh) ',USCell.absLambdaPrimebarPlus_h' num2str(hh) ',USCell.absLambdaPrimebarMinus_h' num2str(hh) '};']);
 eval(['xlswrite(filename,xlsRow,1,''A' num2str(ii+1) ''');']);
 ii = ii+1;
end

